﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis.IData_impl
{
    /// <summary>
    /// 数据接口实现。
    /// </summary>
    /// 创建时间：2022-12-8 09:28:13。作者：王浩力
    public sealed class Gas_analysis_upload_logDAL_impl : BaseDAL, IGas_analysis_upload_logDAL
    {
        /// <summary>
        /// 新增一条
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        /// 创建时间：2022-12-8 09:42:03。作者：王浩力
        public Result Add(Gas_analysis_upload_log info)
        {
            Snowflake snowflake = new Snowflake(1, 1);
            string sql = $@"INSERT INTO gas_analysis_upload_log (
                                        id,
                                        cate,
                                        sample_no,
                                        well_no,
                                        build_name,
                                        top_horizon,
                                        bottom_horizon,
                                        top_well_deep,
                                        bottom_well_deep,
                                        sampling_location,
                                        samping_company_name,
                                        analysis_company_name,
                                        remark,
                                        samping_condition,
                                        client,
                                        samping_time,
                                        analysis_time,
                                        samping_user,
                                        analysis_user,
                                        samping_temperature,
                                        oil_pressure,
                                        casing_pressure,
                                        zong_s,
                                        water_point,
                                        He,
                                        H2,
                                        O2,
                                        N2,
                                        C1,
                                        C6,
                                        C3,
                                        Ic4,
                                        Nc4,
                                        Ic5,
                                        Nc5,
                                        CO2,
                                        C2,
                                        is_desulphurization,
                                        H2Smg,
                                        all_calc_value,
                                        update_time,
                                        update_user,
                                        gas_analysis_key,
                                        data_version
                                    )
                                    VALUES(
                                        {snowflake.NextId()},
                                        $cate,
                                        $sample_no,
                                        $well_no,
                                        $build_name,
                                        $top_horizon,
                                        $bottom_horizon,
                                        $top_well_deep,
                                        $bottom_well_deep,
                                        $sampling_location,
                                        $samping_company_name,
                                        $analysis_company_name,
                                        $remark,
                                        $samping_condition,
                                        $client,
                                        $samping_time,
                                        $analysis_time,
                                        $samping_user,
                                        $analysis_user,
                                        $samping_temperature,
                                        $oil_pressure,
                                        $casing_pressure,
                                        $zong_s,
                                        $water_point,
                                        $He,
                                        $H2,
                                        $O2,
                                        $N2,
                                        $C1,
                                        $C6,
                                        $C3,
                                        $Ic4,
                                        $Nc4,
                                        $Ic5,
                                        $Nc5,
                                        $CO2,
                                        $C2,
                                        $is_desulphurization,
                                        $H2Smg,
                                        $all_calc_value,
                                        $update_time,
                                        $update_user,
                                        $gas_analysis_key,
                                        $data_version
                                    );            ";

            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$cate", info.Cate);
            keyValuePairs.Add("$sample_no", info.Sample_no);
            keyValuePairs.Add("$well_no", info.Well_no);
            keyValuePairs.Add("$build_name", info.Build_name);
            keyValuePairs.Add("$top_horizon", info.Top_horizon);
            keyValuePairs.Add("$bottom_horizon", info.Bottom_horizon);
            keyValuePairs.Add("$top_well_deep", info.Top_well_deep);
            keyValuePairs.Add("$bottom_well_deep", info.Bottom_well_deep);
            keyValuePairs.Add("$sampling_location", info.Sampling_location);
            keyValuePairs.Add("$samping_company_name", info.Samping_company_name);
            keyValuePairs.Add("$analysis_company_name", info.Analysis_company_name);
            keyValuePairs.Add("$remark", info.Remark);
            keyValuePairs.Add("$samping_condition", info.Samping_condition);
            keyValuePairs.Add("$client", info.Client);
            keyValuePairs.Add("$samping_time", info.Samping_time);
            keyValuePairs.Add("$analysis_time", info.Analysis_time);
            keyValuePairs.Add("$samping_user", info.Samping_user);
            keyValuePairs.Add("$analysis_user", info.Analysis_user);
            keyValuePairs.Add("$samping_temperature", info.Samping_temperature);
            keyValuePairs.Add("$oil_pressure", info.Oil_pressure);
            keyValuePairs.Add("$casing_pressure", info.Casing_pressure);
            keyValuePairs.Add("$zong_s", info.Zong_s);
            keyValuePairs.Add("$water_point", info.Water_point);
            keyValuePairs.Add("$He", info.He);
            keyValuePairs.Add("$H2", info.H2);
            keyValuePairs.Add("$O2", info.O2);
            keyValuePairs.Add("$N2", info.N2);
            keyValuePairs.Add("$C1", info.C1);
            keyValuePairs.Add("$C6", info.C6);
            keyValuePairs.Add("$C3", info.C3);
            keyValuePairs.Add("$Ic4", info.Ic4);
            keyValuePairs.Add("$Nc4", info.Nc4);
            keyValuePairs.Add("$Ic5", info.Ic5);
            keyValuePairs.Add("$Nc5", info.Nc5);
            keyValuePairs.Add("$CO2", info.CO2);
            keyValuePairs.Add("$C2", info.C2);
            keyValuePairs.Add("$is_desulphurization", info.Is_desulphurization);
            keyValuePairs.Add("$H2Smg", info.H2Smg);
            keyValuePairs.Add("$all_calc_value", info.All_calc_value);
            keyValuePairs.Add("$update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); // info.Update_time);
            keyValuePairs.Add("$update_user", info.Update_user);
            keyValuePairs.Add("$gas_analysis_key", info.Gas_analysis_key);
            keyValuePairs.Add("$data_version", info.data_version);

            string msg = null;
            int count = ExecSqlCmd(sql, keyValuePairs, c =>
            {
                return c.ExecuteNonQuery();
            },
            ex =>
            {
                msg = ex.Message;
            }
            );
            if (count > 0)
            {
                return new Result("新增成功", 200);
            }
            return new Result("新增出错：" + msg);
        }

        /// <summary>
        /// 根据气分析主键，获取列表
        /// </summary>
        /// <param name="gas_analysis_key">气分析主键</param>
        /// <returns></returns>
        /// 创建时间：2022-12-8 09:42:28。作者：王浩力
        public List<Gas_analysis_upload_log> GetList(long gas_analysis_key)
        {
            string sql = "select  *  from  gas_analysis_upload_log  where  gas_analysis_key=" + gas_analysis_key;
            return ExecSqlCmd(sql, null, c =>
            {
                var reader = c.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                List<Gas_analysis_upload_log> list = new List<Gas_analysis_upload_log>();
                while (reader.Read())
                {
                    Gas_analysis_upload_log info = new Gas_analysis_upload_log();
                    info.Id = reader.GetInt64(0);
                    info.Cate = reader["cate"].ToString();
                    info.Sample_no = reader["Sample_no"].ToString();
                    info.Well_no = reader["Well_no"].ToString();
                    info.Build_name = reader["Build_name"].ToString();
                    info.Top_horizon = reader["Top_horizon"].ToString();
                    info.Bottom_horizon = reader["Bottom_horizon"].ToString();
                    info.Top_well_deep = reader["Top_well_deep"].ToString();
                    info.Bottom_well_deep = reader["Bottom_well_deep"].ToString();
                    info.Sampling_location = reader["Sampling_location"].ToString();
                    info.Samping_company_name = reader["Samping_company_name"].ToString();
                    info.Analysis_company_name = reader["Analysis_company_name"].ToString();
                    info.Remark = reader["Remark"].ToString();
                    info.Samping_condition = reader["Samping_condition"].ToString();
                    info.Client = reader["Client"].ToString();
                    info.Samping_time = reader["Samping_time"].ToString();
                    info.Analysis_time = reader["Analysis_time"].ToString();
                    info.Samping_user = reader["Samping_user"].ToString();
                    info.Analysis_user = reader["Analysis_user"].ToString();

                    info.Samping_temperature = reader["Samping_temperature"].ToMyDecimal();
                    info.Oil_pressure = reader["Oil_pressure"].ToMyDecimal();
                    info.Casing_pressure = reader["Casing_pressure"].ToMyDecimal();
                    info.Zong_s = reader["Zong_s"].ToMyDecimal();
                    info.Water_point = reader["Water_point"].ToMyDecimal();
                    info.He = reader["He"].ToMyDecimal();
                    info.H2 = reader["H2"].ToMyDecimal();
                    info.O2 = reader["O2"].ToMyDecimal();
                    info.N2 = reader["N2"].ToMyDecimal();
                    info.C1 = reader["C1"].ToMyDecimal();
                    info.C6 = reader["C6"].ToMyDecimal();
                    info.C3 = reader["C3"].ToMyDecimal();
                    info.Ic4 = reader["Ic4"].ToMyDecimal();
                    info.Nc4 = reader["Nc4"].ToMyDecimal();
                    info.Ic5 = reader["Ic5"].ToMyDecimal();
                    info.Nc5 = reader["Nc5"].ToMyDecimal();
                    info.CO2 = reader["CO2"].ToMyDecimal();
                    info.C2 = reader["C2"].ToMyDecimal();
                    info.Is_desulphurization = (byte)reader["Is_desulphurization"].ToString().ToMyDecimal();
                    info.H2Smg = reader["H2Smg"].ToString().ToMyDecimal();
                    info.All_calc_value = reader["All_calc_value"].ToString().ToMyDecimal();
                    info.Update_time = reader["Update_time"].ToString();
                    info.Update_user = reader["Update_user"].ToString();
                    info.Gas_analysis_key = (long)reader["Gas_analysis_key"].ToString().ToMyDecimal();
                    info.data_version = (byte)reader["data_version"].ToString().ToMyDecimal();
                    list.Add(info);
                }
                return list;
            });

        }
    }
}
